﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PS_RoyalItemRequests : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IRoyalItemRequestBLL reqBLL = new RoyalItemRequestBLL();

        private RadComboBox cmbEmployeeType, cmbYear;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbEmployeeType = (RadComboBox)cbEmployeeType.FindControl("cbEmployeeType");
            cmbYear = (RadComboBox)cbYear.FindControl("cbYear");

            if (!IsPostBack)
            {
                cbEmployeeType.ListEmployeeTypeComboBox();
                cbYear.ListYear();
                cmbYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
                LoadDataGrid();
            }
        }
        private void LoadDataGrid()
        {
            int Year = Int32.Parse(cmbYear.SelectedValue);
            lblHeader.Text = "รายชื่อผู้มีสิทธิ์ขอรับพระราชทานเครื่องราชอิสริยาภรณ์ ปีงบประมาณ " + Year.ToString();
            IList<RoyalItemRequestInfo> infoList = reqBLL.ListRoyalItemRequest(Year, cmbEmployeeType.SelectedValue);

            RadGrid1.DataSource = infoList;

            GridGroupByExpression g = new GridGroupByExpression(RadGrid1.Columns[3]);
            RadGrid1.MasterTableView.GroupByExpressions.Add(g);

            RadGrid1.DataBind();
        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                reqBLL.Delete(seq, ssMng.EmpSeqI);
            }

            LoadDataGrid();
        }

        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            Session["EMPTYPE"] = cmbEmployeeType.SelectedValue;
            LoadDataGrid();
        }
    }
}
